package com.example.demoes.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.example.demoes.entity.OmsOrder;
import com.example.demoes.service.OmsOrderClientService;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.util.MapUtil;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author: rosszhang
 * @date: 2023/12/9 15:28
 * @description:
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class OmsOrderClientServiceImpl implements OmsOrderClientService {

    private final RestHighLevelClient restHighLevelClient;

    @Override
    public List<OmsOrder> getTemplateByMemberUsername(String memberUsername) throws IOException {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.termQuery("memberUsername", memberUsername));

        // 查询
        SearchRequest searchRequest = new SearchRequest("ross");
        searchRequest.source(sourceBuilder);

        // 查询结果
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        if (RestStatus.OK.equals(searchResponse.status())) {
            // 解析对象
            SearchHit[] hits = searchResponse.getHits().getHits();
            // 获取source
            List<OmsOrder> collect = Arrays.stream(hits).map(item -> {
                return BeanUtil.fillBeanWithMap(item.getSourceAsMap(), new OmsOrder(), false);
            }).collect(Collectors.toList());
            long totalHits = searchResponse.getHits().getTotalHits().value;
            log.info("总数：{}", totalHits);
            return collect;
        }
        return Lists.newArrayList();
    }

    @Override
    public void deleteTemplateById(Long id) throws IOException {
        // 创建删除请求对象
        DeleteRequest deleteRequest = new DeleteRequest("ross");
        deleteRequest.id(String.valueOf(id));
        // 执行删除文档
        DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        log.info("删除状态：{}", response.status());
    }

    @Override
    public long countTemplate() throws IOException {
        CountRequest countRequest = new CountRequest("ross");
        CountResponse count = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
        return count.getCount();
    }
}
